#!/bin/bash

echo "========================================"
echo "           股票推荐系统启动脚本"
echo "========================================"
echo

# 检查Python是否安装
if ! command -v python3 &> /dev/null; then
    echo "❌ 错误: 未检测到Python3，请先安装Python 3.8+"
    echo "Ubuntu/Debian: sudo apt install python3 python3-venv"
    echo "CentOS/RHEL: sudo yum install python3 python3-venv"
    echo "macOS: brew install python3"
    exit 1
fi

# 检查虚拟环境是否存在
if [ ! -d ".venv" ]; then
    echo "📦 创建虚拟环境..."
    python3 -m venv .venv
    if [ $? -ne 0 ]; then
        echo "❌ 创建虚拟环境失败"
        exit 1
    fi
fi

# 激活虚拟环境
echo "🔄 激活虚拟环境..."
source .venv/bin/activate
if [ $? -ne 0 ]; then
    echo "❌ 激活虚拟环境失败"
    exit 1
fi

# 检查依赖是否安装
echo "📋 检查依赖包..."
python -c "import flask, pandas, networkx, matplotlib" &> /dev/null
if [ $? -ne 0 ]; then
    echo "📦 安装依赖包..."
    pip install -r requirements.txt
    if [ $? -ne 0 ]; then
        echo "❌ 依赖包安装失败"
        exit 1
    fi
fi

# 启动应用
echo "🚀 启动股票推荐系统..."
echo
echo "📍 系统将在以下地址启动:"
echo "   http://localhost:5000"
echo "   http://127.0.0.1:5000"
echo
echo "⏳ 正在启动，请稍候..."
echo
python app.py
